<template>
  <div class="container-home">
    <van-nav-bar fixed placeholder>
      <template #left>
        <div class="logo"></div>
      </template>
      <template #right>
        <van-button icon="search" type="primary" round @click="$router.push('/search')">搜索</van-button>
      </template>
    </van-nav-bar>

    <!-- tab栏 -->
    <van-tabs v-model="active" @click="tabClick">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoad"
          >
            <van-cell v-for="item in list" :key="item.art_id" :title="item.title"
            @click="$router.push({path:'/article',query:{id:item.art_id}})">
              <template #label>
                <div>
                  <van-grid :column-num="item.cover.type">
                    <van-grid-item v-for="(value,index) in item.cover.images" :key="index">
                      <van-image
                        :src="value"
                        lazy-load
                      />
                    </van-grid-item>
                  </van-grid>
                  <div>
                    <span>{{item.aut_name}}</span>&nbsp;
                    <span>{{item.comm_count}}评论</span>&nbsp;
                    <span>{{item.pubdate | fromDow}}</span>
                  </div>
                </div>
              </template>
            </van-cell>
          </van-list>
        </van-pull-refresh>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import moment from 'moment';
moment.locale('zh-cn');   
import {getChannelList,getArticleList} from './home.js'
export default {
  name: 'home',
  data () {
    return {
      active: 0,
      loading: false,
      finished: false,
      list: [],
      isLoading: false,
      channels: [],
      timestamp:null
    }
  },
  async created() {
    let res = await getChannelList();
    // console.log(res);
    this.channels = res.data.data.channels
  },
  methods: {
    async onLoad () {
      if(!this.timestamp){
        this.timestamp = Date.now()
      }
      let res = await getArticleList({
        channel_id: this.channels[this.active].id,
        timestamp: this.timestamp
      })
      this.timestamp = res.data.data.pre_timestamp
      this.list.push(...res.data.data.results)
      console.log(res);
      this.loading = false
      if(this.list.length >=50){
        this.finished = true
      }
    },
    onRefresh () {
      this.timestamp = null
      this.finished = false
      this.list = []
      this.loading = true
      this.onLoad()
      this.isLoading = false
    },
    tabClick(){
      this.onRefresh()
    }
  },
  filters: {
    fromDow(value){
      return moment(value).fromNow()
    }
  }
}
</script>

<style lang="less">
.container-home {
  .van-nav-bar{
    z-index: 10;
  }
  .van-nav-bar__content {
    background-color: #049cf5;
    .logo {
      width: 100px;
      height: 30px;
      background: url('~@/assets/logo.png') no-repeat;
      background-size: cover;
    }
    .van-button {
      width: 100px;
      height: 30px;
      background-color: #00b1fe;
      border: 0;
      .van-icon-search {
        color: #fff;
      }
    }
  }
  .van-tabs__wrap {
    position: fixed;
    top: 46px;
    z-index: 10;
    width: 100%;
  }
  .van-tabs__content {
    margin-top: 44px;
  }
}
</style>
